﻿/* Copyright 2010 Igor Francé
 * 
 * This file is part of ECMADoc (http://ecmadoc.codeplex.com).
 * 
 * ECMADoc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * ECMADoc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with ECMADoc. 
 * If not, see http://www.gnu.org/licenses/.
 */
namespace EcmaDoc.Gui.Forms
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Drawing;
	using System.Linq;
	using System.Reflection;
	using System.Windows.Forms;
	using System.Diagnostics;
	using System.IO;

	partial class AboutForm : Form
	{
		private const string AboutResourceName = "EcmaDoc.Gui.Resources.About.rtf";

		public AboutForm()
		{
			this.Font = SystemFonts.MessageBoxFont;
			this.InitializeComponent();

			Stream aboutDescription = GetProductDescription();
			if (aboutDescription != null)
				this.DescriptionBox.LoadFile(aboutDescription, RichTextBoxStreamType.RichText);

			List<ListViewItem> ecmaDocItems = new List<ListViewItem>();
			foreach (ProcessModule module in Process.GetCurrentProcess().Modules)
			{
				DateTime lastWriteDate = File.GetLastWriteTime(module.FileName);

				ListViewItem item = assemblyList.Items.Add(new ListViewItem());
				item.Text = module.ModuleName;
				item.SubItems.Add(module.FileVersionInfo.FileVersion);
				item.SubItems.Add(lastWriteDate.ToString("g"));

				if (module.ModuleName.ToLower().StartsWith("ecmadoc"))
					ecmaDocItems.Add(item);
			}

			foreach (ListViewItem item in ecmaDocItems)
			{
				assemblyList.Items.Remove(item);
				assemblyList.Items.Insert(0, item);
			}
		}

		private Stream GetProductDescription()
		{
			Assembly currentAssembly = Assembly.GetExecutingAssembly();
			foreach (string name in currentAssembly.GetManifestResourceNames())
			{
				if (name == AboutResourceName)
				{
					return currentAssembly.GetManifestResourceStream(name);
				}
			}
			return null;
		}

		private void OnLinkHomePageClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			try
			{
				string link = linkHomePage.Text.Substring(e.Link.Start, e.Link.Length);
				if (!string.IsNullOrEmpty(link))
					System.Diagnostics.Process.Start(link);
			}
			catch (Exception ex)
			{
				MessageBox.Show(string.Format("Unable to open {0}: {1}", e.Link, ex.Message));
			}

		}
	}
}
